<template>
  <el-drawer
    title="组员详情"
    v-model="drawerVisible"
    size="80%"
    :destroy-on-close="true"
  >
    <el-scrollbar>
      <div class="max-height" v-loading="loading">
        <el-descriptions :column="1">
          <el-descriptions-item label="学号"
            >{{ detail?.user?.acc }}
          </el-descriptions-item>
          <el-descriptions-item label="姓名">
            {{ detail?.user?.name }}
          </el-descriptions-item>

          <el-descriptions-item label="角色">
            {{ detail?.user?.identity || "-" }}
          </el-descriptions-item>
          <el-descriptions-item label="项目名称">
            {{ detail?.projectName || "-" }}
          </el-descriptions-item>
          <el-descriptions-item label="第一阶段答辩得分:">
            {{ detail?.db_first || "-" }}
          </el-descriptions-item>
          <el-descriptions-item label="第二阶段答辩得分:">{{
            detail?.db_second || "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="中期答辩得分:">{{
            detail?.db_mid || "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="终期答辩得分:">{{
            detail?.db_terminal || "-"
          }}</el-descriptions-item>
          <el-descriptions-item label="个人最终得分:">{{
            detail?.db_final || "-"
          }}</el-descriptions-item>
          <!-- <el-descriptions-item label="组长">
            {{ detail?.proRequirement || "-" }}
          </el-descriptions-item> -->
        </el-descriptions>
        <h3 style="margin: 20px 0">日报管理</h3>
        <el-collapse v-model="activeLog" accordion>
          <template v-for="(item, index) of logManage" :key="index">
            <el-collapse-item :name="index">
              <template #title>
                <div class="flex-between w-full">
                  <div class="flex align-center">
                    {{ `第${index + 1}周` }}
                    <el-tag
                      v-if="!!item.wrote"
                      type="success"
                      style="margin-left: 10px"
                      >已完成</el-tag
                    >
                    <el-tag v-else type="info" style="margin-left: 10px"
                      >未完成</el-tag
                    >
                    <div v-if="!!item.wrote" style="margin-left: 8px">
                      <span v-if="currenDailyId === item.id">
                        <el-input-number
                          v-model="score"
                          :precision="1"
                          :step="0.1"
                          :max="10"
                        />
                        <el-button
                          style="margin-left: 5px"
                          link
                          @click.stop="submitScore(item)"
                        >
                          <el-icon><Select /></el-icon>
                        </el-button>
                      </span>
                      <el-text v-else style="margin-left: 10px"
                        >(组长打分: {{ item.weekScore || 0 }})
                        <el-button
                          link
                          v-if="roleId === '2'"
                          @click.stop="selfRate(item)"
                        >
                          <el-icon><EditPen /></el-icon>
                        </el-button>
                      </el-text>
                    </div>
                  </div>
                  <el-button
                    v-if="!!item.wrote"
                    class="mr-10"
                    type="primary"
                    size="small"
                    plain
                    @click.stop="toMatrix(item)"
                  >
                    AI 分析
                  </el-button>
                </div>
              </template>
              <el-timeline>
                <template v-for="(log, i) in item.dailyReport" :key="i">
                  <el-timeline-item
                    :timestamp="`${log.date}`"
                    placement="top"
                    v-if="log.report"
                  >
                    <writeDaily :content="log.report" :edit="false" />

                    <div style="margin-top: 10px" v-if="log.report">
                      <div v-if="log.conclude">
                        <h4>AI 总结</h4>
                        <el-text class="flex align-center">
                          {{ log.conclude }}
                        </el-text>
                      </div>
                      <el-text class="flex align-center">
                        自评：{{ log.selfEval }}
                      </el-text>
                    </div>
                  </el-timeline-item>
                </template>
              </el-timeline>
            </el-collapse-item>
          </template>
        </el-collapse>
      </div>
    </el-scrollbar>
    <MatrixDialog ref="MatrixDialogRef" />
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, watch } from "vue";
import api from "@/api";
import MatrixDialog from "@/views/my-project/MatrixDialog.vue";
const drawerVisible = ref(false);

const MatrixDialogRef = ref();
const loading = ref(false);
const detail = ref<any>(null);
const activeLog = ref(0);
const logManage = ref<any>([]);
const score = ref(0);
const currenDailyId = ref("");

const roleId = computed(() => localStorage.getItem("roleId"));
watch(drawerVisible, (bool) => {
  if (!bool) {
    detail.value = null;
  }
});

function submitScore(data: any) {
  const obj = {
    ...data,
    weekScore: score.value,
  };
  updateDaily(obj);
  score.value = 0;
  currenDailyId.value = "";
}
function selfRate(data: any) {
  score.value = data.weekScore;
  currenDailyId.value = data.id;
}

function updateDaily(data: any) {
  loading.value = true;
  api.dailyReport
    .updateWeekReport(data)
    .then((res: any) => {
      if (res.success) {
        loading.value = false;
        getTableData(data.acc);
      }
    })
    .catch(() => {
      loading.value = false;
    });
}

function toMatrix(data: any) {
  MatrixDialogRef.value.open(data);
}

const open = (row: any) => {
  if (row) {
    getTableData(row.acc);
  }
  drawerVisible.value = true;
};

function getTableData(acc: string) {
  loading.value = true;
  api.dailyReport
    .DailyReportByAcc(acc)
    .then((res: any) => {
      if (res.success) {
        detail.value = res.data;
        logManage.value = Object.values(
          Object.keys(res.data).reduce((newData: any, key: string) => {
            if (key.includes("week")) {
              newData[key as string] = res.data[key];
            }
            return newData;
          }, {})
        );
        loading.value = false;
      }
    })
    .catch(() => {
      loading.value = false;
    });
}

defineExpose({ open });
</script>
<style lang="scss" scope>
.max-height {
  height: calc(100vh - 100px);
  padding: 0 16px;
}
</style>
